﻿





namespace Orid.Management.SOHO.Book.ClientTest.Service
{


    using System;
    using System.ServiceModel;

   public class Client
   {

       private  ServiceBookClient bookClient;

       private  ServiceBookClient BookClient
       {
           get
           {
               if (bookClient ==null || bookClient.State == CommunicationState.Closed)
               {

                  
                   WSHttpBinding binding = new WSHttpBinding();
                   binding.ReaderQuotas.MaxArrayLength = 65536000;
                   binding.MaxBufferPoolSize = 65536000;
                   binding.MaxReceivedMessageSize = 65536000;

                   //binding.ReceiveTimeout = TimeSpan.FromMinutes(10);
                   binding.Security.Mode = SecurityMode.Message;


                   bookClient = new ServiceBookClient(binding, new EndpointAddress(wcfAddress));

               }
               return bookClient;
           }
          
       }

       private string wcfAddress;

       /// <summary>
       /// Initializes a new instance of the <see cref="Client"/> class.
       /// </summary>
       /// <param name="uri">Wcf的uri 完整地址</param>
       public Client(string  uri)
        {
            wcfAddress = uri; 
            
        }

       public bool AddSchedule(string operatorID, int accountID, DateTime dateFrom, DateTime dateTo)
       {
           bool bResult;
           try
           {
               if (BookClient.State != CommunicationState.Opened || BookClient.State != CommunicationState.Opening)
               {
                   BookClient.Open();
               }
               bResult = BookClient.AddSchedule(operatorID, accountID, dateFrom, dateTo);
           }
           catch (Exception ex)
           {

               throw ex;
           }
           finally
           {
               BookClient.Close();
           }

           return bResult;


       }

       public bool RemoveSchedule(string operatorID, DateTime dateFrom, DateTime dateTo)
       {
           bool bResult;
           try
           {
               if (BookClient.State != CommunicationState.Opened || BookClient.State != CommunicationState.Opening)
               {
                   BookClient.Open();
               }
               bResult = BookClient.RemoveSchedule(operatorID, dateFrom, dateTo)
               ;
           }
           catch (Exception ex)
           {

               throw ex;
           }
           finally
           {
               BookClient.Close();
           }

           return bResult;
       }


       public DateTime GetCurrentTime()
       {
           DateTime workTime = DateTime.MinValue;
           try
           {
               if (BookClient.State != CommunicationState.Opened || BookClient.State != CommunicationState.Opening)
               {
                   BookClient.Open();
               }
               workTime = BookClient.GetCurrentTime();


              
               
           }
           catch (Exception ex)
           {

               throw ex;
           }
           finally
           {
               BookClient.Close();
           }

           return workTime == DateTime.MinValue?DateTime.Now:workTime;


       }

       public Orid.Management.SOHO.Book.Service.BookSetting[]  GetCurrentBookSetting()
       {
           Orid.Management.SOHO.Book.Service.BookSetting[]  bookSettings;
           
           try
           {
               if (BookClient.State != CommunicationState.Opened || BookClient.State != CommunicationState.Opening)
               {
                   BookClient.Open();
               }
               bookSettings = BookClient.GetCurrentBookSetting();


              
               
           }
           catch (Exception ex)
           {

               throw ex;
           }
           finally
           {
               BookClient.Close();
           }

           return bookSettings;
       }


       public Orid.Management.SOHO.Book.Service.BookRecord[] GetBookList(string operatorID, DateTime bookData)
       {
           Orid.Management.SOHO.Book.Service.BookRecord[] bookRecords;

           try
           {
               if (BookClient.State != CommunicationState.Opened || BookClient.State != CommunicationState.Opening)
               {
                   BookClient.Open();
               }
               bookRecords = BookClient.GetBookList(operatorID, bookData);
 
           }
           catch (Exception ex)
           {

               throw ex;
           }
           finally
           {
               BookClient.Close();
           }

           return bookRecords;
       }


       public DateTime [] GetBookDays(string operatorID, DateTime dateFrom,DateTime dateTo)
       {
           DateTime[] bookDays;

           try
           {
               if (BookClient.State != CommunicationState.Opened || BookClient.State != CommunicationState.Opening)
               {
                   BookClient.Open();
               }
               bookDays = BookClient.GetBookDays(operatorID, dateFrom, dateTo);

           }
           catch (Exception ex)
           {

               throw ex;
           }
           finally
           {
               BookClient.Close();
           }

           return bookDays;
       }

   }
}
